﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PilotProject;

namespace PilotMVC.Controllers
{
    [Authorize(Roles = "Admin")]
    public class TestEngineController : Controller
    {
        private PilotEntities1 db = new PilotEntities1();

        //
        // GET: /TestEngine/

        public ViewResult Index()
        {        
            return View(db.TestEngines.ToList());
        }


        // GET: /TestEngine/Edit/5
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            
            TestEngine testengine = db.TestEngines.Single(t => t.Id == id);
            ViewBag.Questions = testengine.Questions.Where(q => q.IsActive).Count();
            return View(testengine);
        }

      

        //
        // POST: /TestEngine/Edit/5


        [HttpPost]
        public ActionResult Edit(string testEngineDescription, Guid Id, int PassmarkAsPercentage, int DurationInMinutes, int NumberOfQuestions)
        {
            var testengine = db.TestEngines.Where(t => t.Id.Equals(Id)).SingleOrDefault();
                if (ModelState.IsValid)
                {
                    testengine.PassmarkAsPercentage = PassmarkAsPercentage;
                    testengine.TestEngineDescription = testEngineDescription;
                    testengine.DurationInMinutes = DurationInMinutes;
                   
                    testengine.NumberOfQuestions = NumberOfQuestions;
                    testengine.LastUpdated = DateTime.Now;

                    //db.TestEngines.Attach(model);
                    db.ObjectStateManager.ChangeObjectState(testengine, EntityState.Modified);
                    db.SaveChanges();

                    return RedirectToAction("Index", "TestEngine");
                    //return RedirectToActionPermanent("Index");                    
                    //return RedirectToAction("Index", new { Id = Guid.Empty, PassmarkAsPercentage = 0 });             
                }           
                return View(testengine);                              
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}